package com.wys.mall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.wys.mall.order.entity.OrderEntity;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wys.common.utils.PageUtils;
import com.wys.common.utils.Query;

import com.wys.mall.order.dao.OrderItemDao;
import com.wys.mall.order.entity.OrderItemEntity;
import com.wys.mall.order.service.OrderItemService;

@RabbitListener(queues = {"queue.first"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );
        return new PageUtils(page);
    }

    /**
     *
     * 同一个消息，只能有一个人接收
     * 消息按顺序处理，上一个处理完，下一个才开始
     * @Test
     *     void sendMsg() {
     *
     *         for (int i = 0; i < 10 ; i++) {
     *             OrderEntity orderEntity = new OrderEntity();
     *             orderEntity.setBillContent("aaaaaaaa--" + i);
     *             orderEntity.setAutoConfirmDay(i);
     *             orderEntity.setBillHeader("kkkk--" + i);
     *             rabbitTemplate.convertAndSend("exchange.first.direct", "queue.first", orderEntity);
     *         }
     *     }
     * @param message 封装的消息
     * @param orderEntity 发送消息时的类
     * @param channel 通信渠道
     */
//    @RabbitListener(queues = {"queue.first"})
    @RabbitHandler
    public void recieveOrderMessage(Message message, OrderEntity orderEntity, Channel channel)  {
        System.out.println("收到了消息了--->" + message + " ====》内容：" + orderEntity);
        System.out.println("渠道数量：" + channel.getChannelNumber());

        MessageProperties messageProperties = message.getMessageProperties();
        System.out.println("消息处理完成---------------------------------");
        //消息顺序，自增
        long deliveryTag = messageProperties.getDeliveryTag();
        System.out.println(deliveryTag);
        //回复，签收消息, fasle表示只签收当前消息，true签收所有
        try {
            if(deliveryTag % 2== 0){
                //收货
                channel.basicAck(deliveryTag, false);
            }else {
                //退货  不批量拒收， b1：为false时，删除，为true时，重新入队
                channel.basicNack(deliveryTag,  false, false);
//                channel.basicReject(deliveryTag, false);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RabbitHandler
    public void recieveOrderItemMessage(Message message, OrderItemEntity orderItemEntity, Channel channel){
        System.out.println("收到了消息了--->" + message + " ====》内容：" + orderItemEntity);
        System.out.println("渠道数量：" + channel.getChannelNumber());

        MessageProperties messageProperties = message.getMessageProperties();
        System.out.println("消息处理完成---------------------------------");
    }

}